﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace StudentManagementSystem
{
    public partial class Form3 : Form
    {
        private Database db = new Database();
        public Form3()
        {
            InitializeComponent();
            LoadSchools();
        }

        // 加载学校信息
        private void LoadSchools()
        {
            using (var conn = db.GetConnection())
            {
                string query = "SELECT * FROM School";
                MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
                DataTable table = new DataTable();
                adapter.Fill(table);
                dataGridView1.DataSource = table;
            }
        }

        // 添加学校
        private void AddSchool(string schoolName)
        {
            using (var conn = db.GetConnection())
            {
                string query = "INSERT INTO School (SchoolName) VALUES (@schoolName)";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@schoolName", schoolName);

                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("学校添加成功！");
                LogAction($"成功添加学校：{schoolName}");
                LoadSchools();
            }
        }

        // 删除学校
        private void DeleteSchool(int schoolID)
        {
            using (var conn = db.GetConnection())
            {
                string query = "DELETE FROM School WHERE SchoolID = @schoolID";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@schoolID", schoolID);

                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("学校删除成功！");
                LogAction($"成功添加学校,对应id为：{schoolID}");
                LoadSchools();
            }
        }

        // 更新学校信息
        private void UpdateSchool(int schoolID, string newSchoolName)
        {
            using (var conn = db.GetConnection())
            {
                string query = "UPDATE School SET SchoolName = @schoolName WHERE SchoolID = @schoolID";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@schoolName", newSchoolName);
                cmd.Parameters.AddWithValue("@schoolID", schoolID);

                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("学校更新成功！");
                LogAction($"成功更新id为{schoolID}的学校信息,名字修改为{newSchoolName}");
                LoadSchools();
            }
        }

        // 记录用户操作到日志表
        private void LogAction(string action)
        {
            using (var conn = db.GetConnection())
            {
                string query = "INSERT INTO log (Action) VALUES (@action)";
                MySqlCommand command = new MySqlCommand(query, conn);
                command.Parameters.AddWithValue("@action", action);

                conn.Open();
                command.ExecuteNonQuery();
            }
        }

        // 按钮事件：添加学校
        private void btnAddSchool_Click(object sender, EventArgs e)
        {
            string schoolName = txtSchoolName.Text;
            AddSchool(schoolName);
        }

        // 按钮事件：删除学校
        private void btnDeleteSchool_Click(object sender, EventArgs e)
        {
            int schoolID = int.Parse(txtSchoolID.Text);
            DeleteSchool(schoolID);
        }

        // 按钮事件：更新学校
        private void btnUpdateSchool_Click(object sender, EventArgs e)
        {
            int schoolID = int.Parse(txtSchoolID.Text);
            string newSchoolName = txtSchoolName.Text;
            UpdateSchool(schoolID, newSchoolName);
        }
    }
}
